<?php
	class common extends Mysql{
		private $pagec;
		private $prefix;
		function __construct($pagec,$prefix=''){
			$this->prefix = $prefix;
			parent::__construct();
			$this->pagec = $pagec;
		}
		
		/**
		 * 获得数量
		 * @param $table
		 * @param $where
		 * @param $left
		 * @param $group
		 */
		function getNum($sql,$key="",$group=false){
			//是否有group by
			if(strpos($sql, "group by")){
				$group = true;
			}
			
			if($key!="*" && $key!=''){
				$key = "count(*) as num,".$key;
			}else{
				$key = "count(*) as num";
			}
			$sql="select ".$key." from ".$sql;
			if($group==true) $sql = "select count(*) as num from (".$sql.") as test";
			$query=$this->query($sql);
			$row=$this->fetch_array($query);
			$num=$row['num'];
			return $num;
		}
		
		/**
		 * 获得列表
		 * @param $table
		 * @param $key
		 * @param $where
		 * @param $left
		 * @param $page
		 */
		function getList($sql, $sqlnum='', $key="*",$debug='0'){
			$sql="select ".$key." from ".$sql;
			if($debug=='1') echo $sql."<br/>";
			if($sqlnum!=''){
				$totals=$this->getNum($sqlnum,$key);
				$num=10;
				$this->pagec = new page($_GET['page']);
				$this->pagec->pageft($totals,$num);
				if($this->pagec->getfirstcount()<0)
				$this->pagec->setfirstcount(0);
				$query=$this->query($sql." limit ".$this->pagec->getfirstcount().",".$num);
				$return['page']=$this->pagec->getpagenav();
			}else{
				$query=$this->query($sql);
			}
			while($row=$this->fetch_array($query)){
				$result[]=$row;
			}
			$return['result']=$result;
			
			return $return;
		}
		
		/**
		 * 获得状态
		 * @param array $color
		 * @param array $name
		 * @param array $state
		 * @param $key
		 */
		function getState(array $color, array $name, array $result, $key){
			if(is_array($result)){
				foreach($result as $k=>$v){
					$result[$k][$key.'Color'] = $color[$v[$key]];
					$result[$k][$key.'Name'] = $name[$v[$key]];
				}
			}
			
			return $result;
		}
		
		/**
		 * 修改状态
		 * @param $id
		 * @param $key
		 * @param $color
		 * @param $name
		 */
		function changeState($table, $id, $key, array $color, array $name){
			$now = $this->getInfo($table,$id,$key);		//获得目前状态
			
			if($now[$key]=='0') $value='1'; else $value='0';		//判断修改成什么状态
			$state['color'] = $color[$value];		//修改后的颜色
			$state['name'] = $name[$value];			//修更改后的名称
			
			//修改状态
			$arr = array($key=>$value);
			$this->update($table,$arr,"id='".$id."'");
			
			return json_encode($state);
		}
		
		/**
		 * 获得单个内容
		 * @param $table
		 * @param $id
		 * @param $key
		 */
		// function getInfo($table,$id,$key="*",$search_key="id",$debug='0'){
		// 	$sql="select ".$key." from ".$table." where ".$search_key."='".$id."' limit 0,1";
		// 	if($debug=='1') echo $sql."<br/>";
		// 	$query=$this->query($sql);
		// 	$row=$this->fetch_array($query);
			
		// 	return $row;
		// }
		
		/**
		 * 获得单个内容
		 * @param  [type] $sql   
		 * @param  string $key   
		 * @param  string $debug 
		 * @return [type]        
		 */
		function getInfo($sql, $key="*" , $debug='0'){
			$sql=" SELECT ".$key." FROM ". $sql . 'LIMIT 0,1';
			if($debug=='1') echo $sql."<br/>";
			$query=$this->query($sql);
			$row=$this->fetch_array($query);
			
			return $row;
		}
			
		
		/**
		 * 删除
		 * @param $table
		 * @param $id
		 * @param $img
		 * @param $dir
		 */
		function del($table, $id, $file='', $search_key="id"){
			if($id!=""){
				$id=substr($id,0,-1);
				
				if(is_array($file)){
					foreach($file as $k=>$v){
						$key = $v['key'];
						$sql = $table." where ".$search_key." in (".$id.")";
						$keys = $this->getList($sql,'',$key);
						$keys = $keys['result'];
						if(is_array($keys)){
							foreach($keys as $key=>$val){
								@unlink($v['dir'].$val[$v['key']]);
							}
						}
					}
				}
				
				$sql="delete from `".$table."` where ".$search_key." in (".$id.")";
				$result = $this->query($sql);
			}else{
				$result = '0';
			}
			return $result;
		}
		
		/**
		 * 上传文件
		 * @param $name
		 * @param $dir
		 */
		function uploadFile($name,$dir){
			$File = upload::uploadfile($ARG=array(
			'File'     => array('name'=>$_FILES[$name]['name'],'tmp_name' => $_FILES[$name]['tmp_name']),
			'Dir'=>$dir
			));
			
			$file=$File['newname'];
			
			return $file;
		}

		/**
		 * ajax提交结果
		 * @param $statusCode
		 * @param $message
		 * @param $navTabId
		 * @param $rel
		 * @param $callbackType
		 * @param $forwarUrl
		 * @param $confirmMsg
		 */
		function ajaxDone($message,$statusCode="200",$forwardUrl="",$callbackType="forward",$navTabId="",$rel="",$confirmMsg=""){
			$result = array(
							"statusCode"=>$statusCode,
							"message"=>$message,
							"navTabId"=>$navTabId,
							"rel"=>$rel,
							"callbackType"=>$callbackType,
							"forwardUrl"=>$forwardUrl,
							"confirmMsg"=>$confirmMsg
						);
			return json_encode($result);
		}
		
		/**
		 * 保存提示
		 * @param $msg
		 * @param $url
		 */
		function submitAlert($msg,$url){
			return "<script>
					alert('".$msg."');
					window.location.href='".$url."';
				   </script>";
		}
		
		/**
		 * 转换post，get提交
		 * @param $arr
		 */
		function strtohtml($arr){
		 	foreach($arr as $k=>$v){
				if(is_array($v)){
					$arr[$k]=self::strtohtml($v);
				}else{
					$arr[$k]=mysql_real_escape_string($v);
				}
			}
		 	return $arr;
 		}
 		
 		/**
 		 * 定时跳转
 		 * @param $URL
 		 * @param $content
 		 * @param $statime
 		 */
		function refreshto($URL,$content,$statime=0){
			global $smarty;
			$smarty->assign('statime',$statime);
			$smarty->assign('URL',$URL);
			$smarty->assign('content',$content);
			$smarty->display('refreshto.html');
		}
		
		/**
		 * 获得省市区
		 * @param $type
		 * @param $father
		 * @param $selected
		 */
		function getArea($type,$father='',$selected=''){
			$sql="from pcc_".$type." where fatherid='".$father."'";
			$result = $this->getList($sql);
			$child = $result['result'];
			if(is_array($child)){
				foreach($child as $k=>$v){
					if($selected==$v[$type.'id']) $child[$k]['selected']=' selected="selected"';
				}
			}
			return $child;
		}
		
		/**
		 * 记录A的序号
		 * @param $table
		 * @param $id
		 */
		function sortA($table,$id){
			$sql = $table . ' WHERE id ="'.$id.'"';
			$sort = $this->getInfo($sql, "sort");
			$sort = $sort['sort'];
			return $sort;
		}
		
		/**
		 * 记录B的序号
		 * @param $table
		 * @param $sortA
		 * @param $type
		 */
		function sortB($table,$sortA,$type,$where='',$join=''){
			//如果是倒序排列，上一个是prev，如果是正序排列，上一个是next
			$key = "t.id,t.sort";
			if($type=="prev") $sql = "`".$table."` t ".$join." where t.sort>".$sortA.$where." order by t.sort asc";
			if($type=="next") $sql = "`".$table."` t ".$join." where t.sort<".$sortA.$where." order by t.sort desc";
			$sortB = $this->getList($sql,'',$key);
			return $sortB['result']['0'];
		}
		
		/**
		 * 交换序列
		 * @param $table
		 * @param $id
		 * @param $type
		 */
		function sortChange($table,$id,$type,$where="",$join=""){
			$sortA = $this->sortA($table, $id);
			$sortB = $this->sortB($table, $sortA, $type,$where,$join);
			if($sortB['id']!=''){
				//修改A的序号
				$arrA = array('sort'=>$sortB['sort']);
				$this->update($table,$arrA,"id='".$id."'");
				//修改B的序号
				$arrB = array('sort'=>$sortA);
				$this->update($table,$arrB,"id='".$sortB['id']."'");
			}
		
			return $sortB;
		}
	}
?>